﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.DictionaryHelper;
using Honestysoft.BaseObject;
using Honestysoft.BaseLibrary.ExceptionHandling;

namespace Honestysoft.Financing.WebUI.Module.Contract
{
    public partial class ContractCategory_Edit : BasePage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                BindControl();
                Bind();
            }
        }

        private void BindControl()
        {
            
        }

        private void Bind()
        {
            Sys_Dictionary obj = null;
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                obj = new BaseBusinessLogic<Sys_Dictionary>().GetEntity(Request.QueryString["ID"]);
                numSortCode.Text = obj.SortCode.ToString();
                txtDictName.Text = obj.DictName;
                hidDictValue.Value = obj.DictValue;
                chkIsValid.Checked = obj.IsValid;
                chkIsDefault.Checked = obj.IsDefault;
                txtRemark.Text = obj.Remark;
            }
            else
            {
                obj = Honestysoft.DictionaryHelper.DictionaryInstance.GetNextDictionary("ContractCategory");
                numSortCode.Text = obj.SortCode.ToString();
                hidDictValue.Value = obj.DictValue;
                chkIsValid.Checked = true;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                Edit();
            }
            else
            {
                Add();
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        private void Add()
        {
            try
            {
                Sys_Dictionary obj = GetSubmitObject();
                int ret = Honestysoft.DictionaryHelper.DictionaryInstance.CreateDictionary(obj);
                if (ret == 1)
                {
                    ScriptHelper.Window.AlertClose("保存成功！", true);
                    Honestysoft.DictionaryHelper.DictionaryInstance.RemoveDictionary();
                }
                else
                {
                    ScriptHelper.Window.Alert("保存失败！");
                }
            }
            catch (BaseException bex)
            {
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("保存失败！系统发生了未知错误！");
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        private void Edit()
        {
            try
            {
                Sys_Dictionary obj = GetSubmitObject();
                int ret = Honestysoft.DictionaryHelper.DictionaryInstance.UpdateDictionary(obj);
                if (ret == 1)
                {
                    ScriptHelper.Window.AlertClose("保存成功！", true);
                    Honestysoft.DictionaryHelper.DictionaryInstance.RemoveDictionary();
                }
                else
                {
                    ScriptHelper.Window.Alert("保存失败！");
                }
            }
            catch (BaseException bex)
            {
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("保存失败！系统发生了未知错误！");
            }
        }
        /// <summary>
        /// 得到提交对象
        /// </summary>
        /// <returns></returns>
        private Sys_Dictionary GetSubmitObject()
        {
            Sys_Dictionary obj = new Sys_Dictionary();
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                obj.ID = Request.QueryString["ID"];
            }
            else
            {
                obj.ID = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
            }
            obj.CategoryID = "ContractCategory";
            if (numSortCode.Text.Trim().Length > 0) { obj.SortCode = Convert.ToInt32(numSortCode.Text); }
            obj.DictName = txtDictName.Text;
            obj.DictValue = hidDictValue.Value;
            obj.IsValid = chkIsValid.Checked;
            obj.IsDefault = chkIsDefault.Checked;
            obj.Remark = txtRemark.Text;
            return obj;
        }
    }
}